﻿@{
    ViewBag.Title = "SimpleSub";
    Layout = "~/Views/Shared/_CwfPage.cshtml";
}

<h2>Simple Subscribe</h2>
<br />
<h3>Path: @ViewBag.Path</h3>
<br />
<h3>See the result on console</h3>
<br />



<pre>
    The subscribe method is quite simple. 
    All you need to do is give the function a topic and a callback. 
    If the topic that you specified gets published to, the callback will be executed 
    (in the context of an empty object or the specified context, meaning this refers to {} 
    or the specified context object). 
    You may include more than one topic to subscribe the callback to just by adding a space and listing another topic. 
    If there are multiple callbacks subscribed to one topic, they will be executed in the order that they were subscribed. 
</pre> 

<input id="btnHandler1" type="button" value="foo" />
<input id="btnHandler2" type="button" value="foo bar baz" />
<input id="btnHandler3" type="button" value="foo context" />

<script type="text/javascript">
    $(document).ready(function () {
        // Subscribe to a single topic called 'foo'
        var handler1 = $.subscribe("foo", function (topic, data) {
            ZEUS.DEBUG.log("Hanlder1: " + topic + ", Data: " + data);
        });

        // Subscribe to multiple topics at once
        // 'foo', 'bar', and 'baz' are three different topics
        var handler2 = $.subscribe("foo bar baz", function (topic, data) {
            ZEUS.DEBUG.log("Hanlder2: " + topic + ", Data: " + data);
        });

        // Subscribe with a context
        // Callback now has its this keyword assigned to the specified object
        var obj = {
            data: 0,
            func: function (topic, data) {
                ZEUS.DEBUG.log("Hanlder3: " + topic + ", Data: " + data);
            }
        };

        var handler3 = $.subscribe("baz", obj.func, obj);

        $('#btnHandler1').click(function () {
            // Publish to some topics without any data
            $.publish("foo bar");
            ZEUS.DEBUG.log("*********************************");
        });

        $('#btnHandler2').click(function () {
            // Publish to a single topic with some data
            $.publish("foo", "test");
            ZEUS.DEBUG.log("*********************************");
        });

        $('#btnHandler3').click(function () {
            $.publish("bar");
            $.publish("baz", "context data");
            ZEUS.DEBUG.log("*********************************");
        });
    });
</script>